package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 本例中列举了Cookie类的基本用法，请直接访问本Servlet即可看到运行结果。
 * @author Steven Suo
 *
 */
@WebServlet("/cookie.view")
public class CookieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CookieServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unused")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		Cookie cookie = new Cookie("visitTime",	new Date().toString());
		response.addCookie(cookie);
		HttpSession session = request.getSession();
		Cookie[] cookies = request.getCookies();
		if (cookies == null) {
			out.println("当前没有任何Cookie");
		}else{
			out.println("<p><Strong>所有的Cookie如下：</Strong></p>");
			for (Cookie c : cookies) {
				out.print("<p>"+c.getName()+" === "+c.getValue()+"</p>");
			}
		}
		out.close();
	}

}
